package br.edu.udc.JMonster.view.delegate.endereco
{	
	import br.edu.udc.JMonster.view.delegate.IDelegate;
	import br.edu.udc.JMonster.view.dto.endereco.Pais;
	import br.edu.udc.JMonster.view.model.AbstractModelLocator;
	import br.edu.udc.JMonster.view.model.endereco.PaisModelLocator;
	
	import com.adobe.cairngorm.business.ServiceLocator;
	import com.adobe.cairngorm.vo.IValueObject;
	
	import mx.collections.IList;
	import mx.rpc.IResponder;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;

	/**
	 * 2008 - ApolloTi Desenvolvimento de Software LTDA. 
	 * Todos os direitos reservados.
	 * 
	 * @author Rodrigo P. Fraga
	 * @email rodrigo@apollo-ti.com
	 * @since 02/05/2008
	 * @version 1.0
	 * @category Delegate
	 */
	public class PaisDelegate implements IDelegate
	{
		/*-------------------------------------------------------------------
	 	*				 		    ATTRIBUTES
	 	*-------------------------------------------------------------------*/
		protected var responder:IResponder;
		protected var service:RemoteObject;
		protected var paisModel:PaisModelLocator = PaisModelLocator.getInstance();
		
		/*-------------------------------------------------------------------
	 	*				 		    CONSTRUCTOR
	 	*-------------------------------------------------------------------*/	
		public function PaisDelegate()
		{
			this.service = ServiceLocator.getInstance().getRemoteObject("paisDestination");
			this.addEventListeners();
		}
		
		/*-------------------------------------------------------------------
	 	*				 		     BEHAVIORS
	 	*-------------------------------------------------------------------*/
		private function addEventListeners():void
		{
			this.service.getOperation("save").addEventListener(ResultEvent.RESULT, onSave);
			this.service.getOperation("remove").addEventListener(ResultEvent.RESULT, onRemove);
			this.service.getOperation("getListLike").addEventListener(ResultEvent.RESULT, onGetListLike);
			this.service.getOperation("findById").addEventListener(ResultEvent.RESULT, onFindById); 
		}
		
		public function save(pais:IValueObject):void
		{
			this.service.save(pais);
		}
		
		public function remove(pais:IValueObject):void
		{			
			this.service.remove(pais);
		}

		public function getListLike(pais:IValueObject):void
		{
			this.service.getListLike(pais);
		}
		
		public function findById(pais:IValueObject):void
		{
			this.service.findById(pais);
		}
				
		/*-------------------------------------------------------------------
	 	*				 		     RESPONDERS
	 	*-------------------------------------------------------------------*/
	 	private function onSave(event:ResultEvent):void
		{
			this.service.removeEventListener(ResultEvent.RESULT, onSave);
			
			this.paisModel.entity = new Pais();
			this.paisModel.status = "Salvo com sucesso!";
			PaisModelLocator.entitySelecionado = Pais(event.result);
		}
		
		private function onRemove(event:ResultEvent):void
		{
			this.service.removeEventListener(ResultEvent.RESULT, onRemove);
			
			this.getListLike(new Pais());
			this.paisModel.entity = new Pais();
			this.paisModel.currentState = AbstractModelLocator.PESQUISA_STATE;
			
			this.paisModel.status = "Excluído com sucesso!";
		}
		
		private function onGetListLike(event:ResultEvent):void
		{
			this.service.removeEventListener(ResultEvent.RESULT, onGetListLike);
			this.paisModel.list = IList(event.result);
		}

		private function onFindById(event:ResultEvent):void
		{
			this.service.removeEventListener(ResultEvent.RESULT, onFindById);
			this.paisModel.entity = Pais(event.result);
		}
	}
}